<?php
/*
 * Created on 2012-9-6
 * Creater :cnluckylee
 * Creater mail:cnlucklee@gmail.com
 */
  class Stat_model extends CI_Model{

 	public function __construct()
 	{
 		$this->load->database();
 		$this->load->library('user_agent');
 		$this->load->model('anti_model');
 	}
 	//写入访问记录数据库
 	public function insertdata()
 	{
 		$data_tmp = Tools::url2Array();
 		/*url数据整理*/
 		$data = array();
 		$data['wid'] = $data_tmp['wid'];
 		$data['refer'] = $data_tmp['refer'];
 		$data['arriveCount'] = $data_tmp['arriveCount'];
 		$data['arriveRefer'] = $data_tmp['arriveRefer'];
		$data['arriveTime'] = time();
 		$tj_eid =  trim($this->input->get("tj_eid"));
 		$eid = explode('-',$tj_eid);
		$data['vid'] = intval($eid[0]);
 		$data['firstTime'] = intval($eid[1]);
 		$url = $data_tmp['refer'];   //获取完整的来路URL
 		if($url)
 		{
 			$str = str_replace("http://","",$url);  //去掉http://
			$strdomain = explode("/",$str);               // 以“/”分开成数组
			$data['refer_domain']   = trim($strdomain[0]);              //取第一个“/”以前的字符
 		}
		$uid = intval($data_tmp['uid']);
 		$data['ip'] =  $this->input->ip_address();

 		/*解析ip地址*/
 		include_once(APPPATH.'libraries/geoipcity/geoipcity.inc');
 		include_once(APPPATH.'libraries/geoipcity/geoipregionvars.php');
 		$gi = geoip_open(APPPATH."libraries/geoipcity/GeoLiteCity.dat",GEOIP_STANDARD);
		$record = geoip_record_by_addr($gi,$data['ip']);
		if($record)
		{
	 		$data['city'] = $record->city;
		}

		/*检查是否是新用户*/
		if(self::isNewVid($data['vid'],$data_tmp['ntime']))
 		{
 			$vistor_data = array();
 			$vistor_data = $data;
 			if($record)
			{
	 			$vistor_data['province'] = $GEOIP_REGION_NAME[$record->country_code][$record->region];
	 			$vistor_data['region'] = $record->region;
			}
 			$vistor_data['lg'] = $data_tmp['lg'];
 			$vistor_data['showp'] = $data_tmp['showp'];
			$vistor_data['st'] = $data_tmp['st'];
 			self::firstArrive2Tj_Vistor($vistor_data);
 			unset($vistor_data);
 		}
 		$data['stopTime'] = intval($data_tmp['st']);
 		unset($data_tmp);
		/*检查是否是从别的网站过来的*/
 		if ($data['refer'])
		{
		    $search_keyword =  Tools::getKeywordBySearch($data['refer'],$data['wid']);
			$data['searchEngine'] = $search_keyword['searchEngine'];
			$data['keyword'] = $search_keyword['keyword'];
			if($search_keyword['price']>0)
			{
				$data['iskeyword'] =1;
				$search_keyword['wid'] = $data['wid'];
				$this->anti_model->pay2tj_losebill($search_keyword);
			}
		}
		/*检测是否是本站会员*/
		$data_new = array();
 		$data_new = self::checkWebsite($data['wid'],$data['vid'],$uid,$data);

		if(empty($data_new))
		{
			return ;
		}
		unset($data_new['bad_count'],$data_new['eyi_state']);

	 	$this->db->insert('tj_stat',$data_new);
		return null;
 	}




 	/**
 	 * 判定是否是第一次访问
 	 * 新访客返回true，否则返回false
 	 * */
 	public function isNewVid($vid,$firstArrive)
 	{

 		$vid_arr = $this->redisinit->get('vid_'.$vid);
 		if(!empty($vid_arr))
 		{
 			/*更新最新访问时间*/
 			$this->db->update('tj_vistor',array('updTime'=>time()),array('vid'=>$vid));
 			if(date("Y-m-d") != date("Y-m-d",$vid_arr['todayFirstTime']))
 			{
 				/*设置该访客第一次访问时间*/
	 			$vid_arr['todayFirstTime'] = $firstArrive;
	 			$vid_arr['arriveTimes'] = $vid_arr['arriveTimes']+1;
 			}else{
 				$vid_arr['arriveTimes'] = $vid_arr['arriveTimes']+1;
 			}
 			$this->redisinit->set('vid_'.$vid,$vid_arr,86400);
 			return false;
 		}else{
 			/*设置该访客第一次访问时间*/
 			$vid_arr = array('todayFirstTime'=>$firstArrive,'arriveTimes'=>1);
 			$this->redisinit->set('vid_'.$vid,$vid_arr,86400);
 			return true;
 		}
 	}

 	/**
 	 * 第一次访问写入访客表
 	 */
 	public function firstArrive2Tj_Vistor($data)
 	{
 		$data['browser'] = $this->agent->browser();
 		$data['browser_ver'] = $this->agent->version();
 		$data['OS'] = $this->agent->platform();
 		$data['browser_Name'] = $data['browser'].' '.$data['browser_ver'];
 		$data['OSLang'] = $data['lg'];
 		$data['fenbianlv'] = $data['showp'];
 		$data['userAgent'] = $this->agent->agent_string();
 		$data['updTime'] = $data['firstTime'];
 		unset($data['lg'],$data['arriveTime'],$data['refer'],
 			  $data['rtime'],$data['showp'],$data['st'],
 			  $data['refer_domain'],$data['arriveRefer']);

 		$this->db->insert('tj_vistor',$data);
 	}


	/**
	 * 检测网站
	 * 非本站会员不写入，本站会员当天访问总数+1
	 */
	 function checkWebsite($wid,$vid,$uid,$data)
	 {
		/*更新当天的访问数*/
		$website = $this->redisinit->get('website_'.$uid.'_'.$wid);
		if(!empty($website))
		{
			/*访问数加1*/
			$website[date('Y-m-d')] = @$website[date('Y-m-d')]+1;
			$this->redisinit->set('website_'.$uid.'_'.$wid,$website);

				//非第一次访问
				$new_data = $this->redisinit->get('wid_vid_'.$wid.'_'.$vid);
				$setting_time = self :: returnTimes($wid);
				if(isset($new_data))
				{
					$new_data['stopTime'] = $data['stopTime']>900?900:$data['stopTime'];
					$new_data['arriveCount'] = $data['arriveCount']+1;
					$data['isbad'] = isset($data['iskeyword'])?1:null;
					$data['bad_count'] = $new_data['bad_count'] = $new_data['bad_count']+1;
					$data['eyi_state'] = isset($data['iskeyword'])?self::clickstate($wid,$new_data):0;
					$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$data,$setting_time);
					unset($data);
					return $new_data;
				}else{
					//第一次访问
					$data['isbad'] = 1;
					$data['bad_count'] = 1;
					$data['eyi_state'] = 0;
					$this->redisinit->set('wid_vid_'.$wid.'_'.$vid,$data,$setting_time);
					unset($data);
					return null;
				}
		}
		return null;
	 }
	  /**
	   * 判断有效无效点击
	   */
	   public function clickstate($wid,$data)
	   {
	   		$state = 0;
			$setting = $this->redisinit->get('setting_'.$wid);
			if($setting['eyi_radio'] == 1)
			{
				/*24小时内满足几次就警告*/
				if($setting['close_24']< $data['bad_count'])
				{
					$state = 11;
				}
			}else if($setting['eyi_radio'] == 2)
			{
				if((date('Y-m-d',$data['arriveTime'])-$data['start_date'])>$setting['close_hours']  && $data['bad_count'] >= $setting['close_Times_Jinggao'])
				{
					if($data['bad_count'] < $setting['close_Times_Zuzhi'])
					{
						$state = 12;
					}else{
						$state = 13;
					}
				}
			}

			return $state;
	   }


	/**
	 * 根据设置返回应设置的保存时间
	 **/
	public function returnTimes($wid) {
		$setting = $this->redisinit->get('setting_' . $wid);
		if ($setting['eyi_radio'] == 2) {
			return $setting['close_Times_Zuzhi'] * 86400;
		} else {
			return 86400; //一天
		}
	}



  }
?>
